angular.module('BaseControl')

.directive("label", [function() {
    return {
        restrict: "E",	// 指令是一个元素 (并非属性)，备选项有"A"、"E" 和 "C"， "M" ,分别代表attribute、element、class和comment（默认值为"A"）
        scope: {
        },
        template: 		// 替换HTML (使用scope中的变量)
            '<div class="Label">' + 
                '<div ng-class="Class" style="float:left;" ng-click="textClick()" ng-show="Visible" ng-transclude></div>' + 
                '<div ng-show="!IsEmpty" style="float:left;padding:3px 0px 0px 2px;color:red;">*</div>' + 
            '</div>',
        replace: true,			// 使用模板替换原始标记
        transclude: true,		// 不复制原始HTML内容
        link: function (scope, element, attrs) {
            scope.Enabled = true;
            scope.Visible = true;
            scope.Class = '';
            scope.IsEmpty = true;

            scope.textClick = function(){
                if(scope.Class != ""){
                    eval('scope.$parent.' + attrs["click"]);
                }
            };

            if (attrs["isempty"] != undefined && attrs["isempty"].toLocaleLowerCase() == "false") {
                scope.IsEmpty = false;
            }
            if (attrs["click"] != undefined && attrs["click"].toLocaleLowerCase() != "") {
                scope.Class = "LabelUnderline";
            }

            scope.$watch(
                function(){
                    return attrs["enabled"];
                },
                function(){
                    if (attrs["enabled"] != undefined && attrs["enabled"].trim().toLocaleLowerCase() == "false") {
                        scope.Enabled = false;
                    }
                    else{
                        scope.Enabled = true;
                    }
                }
            );

            scope.$watch(
                function(){
                    return attrs["visible"]
                },
                function(){
                    if (attrs["visible"] != undefined && attrs["visible"].trim().toLocaleLowerCase() == "false") {
                        scope.Visible = false;
                    }
                    else{
                        scope.Visible = true;
                    }
                }
            );

            scope.$watch(
                function(){
                    return attrs["isempty"]
                },
                function(){
                    if (attrs["isempty"] != undefined && attrs["isempty"].trim().toLocaleLowerCase() == "false") {
                        scope.IsEmpty = false;
                    }
                    else{
                        scope.IsEmpty = true;
                    }
                }
            );
        }
    }
}]);
